package com.yimils.core.sys.beanUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yimils.core.sys.entity.UserEntity;
import com.yimils.core.sys.entity.UserRoleEntity;
import com.yimils.core.sys.entity.UserRppEntity;
import com.yimils.core.sys.service.UserRoleService;
import com.yimils.core.sys.service.UserRppService;
import com.yimils.core.sys.service.UserService;
import com.yimils.core.sys.vo.PartnerVo;
import com.yimils.core.sys.vo.UserVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Component
public class PartnerVoBeanUtils {

    @Resource(name = "userRoleService")
    private UserRoleService userRoleService;

    @Resource(name = "userService")
    private UserService userService;

    @Resource(name = "userRppService")
    private UserRppService userRppService;

    public IPage<PartnerVo> deepClone(IPage<UserEntity> src) {
        IPage<PartnerVo> pages = new Page<>();
        pages.setCurrent(src.getCurrent());
        pages.setSize(src.getSize());
        pages.setTotal(src.getTotal());
        pages.setPages(src.getPages());
        pages.setRecords(cloneRecords(src.getRecords()));
        return pages;
    }


    public List<PartnerVo> cloneRecords(List<UserEntity> src) {
        List<PartnerVo> recordsVo = new ArrayList<>();
        for (UserEntity userEntity : src) {
            PartnerVo vo = new PartnerVo();
            vo.setId(userEntity.getId());
            vo.setName(userEntity.getName());
            vo.setNick(userEntity.getNick());
            vo.setMobile(userEntity.getMobile());
            vo.setHotelNum(0);
            vo.setDeviceNum(0);
            vo.setFeederNum(0);
            QueryWrapper<UserRppEntity> qw = new QueryWrapper<>();
            qw.lambda().eq(UserRppEntity::getUserId, userEntity.getId());
            vo.setRpp(userRppService.list(qw));
            vo.setBalance(userEntity.getBalance());
            vo.setGender(userEntity.getGender());
            vo.setSuperiorId(userEntity.getSuperiorId());
            UserEntity superiorEntity = userService.getById(userEntity.getSuperiorId());
            if (superiorEntity != null) {
                vo.setSuperiorName(superiorEntity.getNick());
            }
            vo.setTeamNum(0L);
            vo.setTeamDevNum(0L);
            vo.setActive(userEntity.getActive());
            vo.setCreateTime(userEntity.getCreateTime());
            QueryWrapper<UserRoleEntity> qw2 = new QueryWrapper<>();
            qw2.lambda().eq(UserRoleEntity::getUserId, userEntity.getId());
            UserRoleEntity userRoleEntity = userRoleService.getOne(qw2);
            vo.setType(userRoleEntity.getType());
            vo.setInviteCode(userEntity.getInviteCode());
            recordsVo.add(vo);
        }
        return recordsVo;
    }

}
